import type { LoginResult } from '@/service/login'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useMemberStore = defineStore(
  'member',
  () => {
    const profile = ref<LoginResult>()

    const setProfile = (val: LoginResult) => {
      profile.value = val
    }

    const clearProfile = () => {
      profile.value = undefined
    }
    // 更新头像
    const updateAvatar = (avatar: string) => {
      profile.value!.avatar = avatar
    }
    // 更新昵称
    const updateNickname = (nickname: string) => {
      profile.value!.nickname = nickname
    }

    return {
      profile,
      setProfile,
      clearProfile,
      updateAvatar,
      updateNickname,
    }
  },
  {
    persist: {
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value) // [!code warning]
        },
        getItem(key) {
          return uni.getStorageSync(key) // [!code warning]
        },
      },
    },
  },
)
